﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using AwesomeGame.UserInput;

namespace AwesomeGame.Screens.Controls
{
    public class MessageSender
    {
        public delegate void MessageEvent(string message);

        public event MessageEvent SendMessage;

        private string _message;
        private Vector2 _position;
        private SpriteFont _font;
        public bool Visible;
        private KeyboardHelper _keyboard;

        public MessageSender(Vector2 position)
        {
            _position = position;
            _message = "";
            _font = Game1.ContentManager.Load<SpriteFont>(@"Fonts\candc16pt");
            Visible = false;
            _keyboard = new KeyboardHelper(new List<Keys> { Keys.Enter, Keys.Escape, Keys.Space });
            _keyboard.KeysOfInterest.AddRange(KeyboardHelper.GetAllNonFancyKeys());
            _keyboard.KeyReleased += new KeyboardHelper.KeyboardEvent(_keyboard_KeyReleased);
        }

        void _keyboard_KeyReleased(KeyboardHelper sender, Keys key)
        {
            if (key == Keys.T && Visible == false)
            {
                _message = "";
                Visible = true;
            }
            else if (key == Keys.Escape)
                Visible = false;
            else if (key == Keys.Enter && String.IsNullOrEmpty(_message) == false)
            {
                if (SendMessage != null)
                    SendMessage(_message);
                Visible = false;
            }
            else
            {
                string keyString = key.ToString();
                if (key == Keys.Space)
                    keyString = " ";
                _message += keyString;
            }
        }

        public void Update(GameTime gameTime)
        {
            _keyboard.Update(gameTime);
        }

        public void Draw(GameTime gameTime)
        {
            if (Visible)
                Game1.SpriteBatch.DrawString(_font, _message + "_", new Vector2(_position.X, _position.Y), Color.Green);
        }
    }
}
